<?php
/**
 * Created by PhpStorm.
 * User: DR
 * Date: 2018/8/2
 * Time: 16:34
 */

namespace app\api\model;

use think\Db;

class User  extends BaseModel
{
    protected $visible = ['id','nickname','avatar'];

    //转账cny
    /*
     * @param object $user  转账人
     * @param object $transfer收账人
     * @param int $cny
     */
    public static function targeCny($user,$transfer,$cny)
    {
        parent::dealUserMoney($user,$cny*-1);
        $desc = '给ID:' . $transfer->id . '转账CNY:'. $cny;
        account_log($user->id,$cny * -1,$user->cny,'',$user->sac,$desc);
        $transfer->cny = $transfer->cny + $cny;
        $transfer->total_cny += $cny;
        if($transfer->save())
        {
            $desc = '收到ID:' . $user->id . '转账CNY:' . $cny;
            account_log($transfer->id,$cny,$transfer->cny,'',$user->sac,$desc);
        }
    }


    //sac兑换cny
    /*
     * @param object $user 用户
     * @param array $data sac价格和数量
     */
    public static function exchangeSac($user,$data)
    {
        $cny = round($data['sacnum'] * $data['sacprice'],4);
        $user->sac = $user->sac - $data['sacnum'];
        $user->cny = $user->cny + $cny;
        $user->save();
        $desc = 'SAC:'.$data['sacnum'].'兑换CNY:'.$cny;
        account_log($user->id,$cny,$user->cny,$data['sacnum']*-1,$user->sac,$desc);
        platform_log($user->id,$cny*-1,$desc);
    }

    //获取直推团队
    public static function getMyTeam($user_id,$listRows,$page)
    {
        $users = self::where('first',$user_id)
         	->where('is_buymill',1)
            ->paginate($listRows,false,[
                'page' => $page,
            ]);
        return $users;
    }

    //获取三级总人数和
    public static function getMyTeamCount($user_id)
    {
        $first = self::where('first',$user_id)->where('is_buymill',1)->count();
//        $second = self::where('second',$user_id)->where('is_buymill',1)->count('id');
//        $third = self::where('third',$user_id)->where('is_buymill',1)->count('id');
        $son_id = Db::name('user_recommend')->where('user_id','=',$user_id)->find()['son_id'];
        $count = self::where(
            [
                'id'=>['in',$son_id],
                'is_buymill' => '1'
            ]
        )->count();
       //找没有矿机的
        $where['id'] = ['in',$son_id];
        $where['is_buymill'] = 0;
        $where['del'] = 1;
        $list = db('user')->where($where)->select();
        $num = 0;
        foreach($list as $k=>$v){
            $r = db('user_equity')->where(['user_id'=>$v['id']])->find();
            if($r){
                $num+=1;
            }
        }
        //团队股权总数
        $where1['user_id'] = ['in',$son_id];
        $where1['status'] = 1;
        $money = db('user_equity')->where($where1)->sum('money');
      	if($user_id==983416){
            $money = 3884521.5841;
        }
        $count = [
            'count' => $count+$num,
            'directnum' => $first,
            'money' => $money,
        ];
        return $count;
    }

//    public function getIsCertificationAttr($value)
//    {
//        $status = [ 0=>'未认证',1=>'已认证' ];
//        return $status[$value];
//    }

    public function getAvatarAttr($value)
    {
        return $value?$value:config('default_avatar');
    }
}